package org.m3;


import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.LayoutInflater.Factory;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class Home extends Activity implements OnClickListener {
	public static boolean soundEnabled = true;
   
	private final String LANG_KEY = "org.m3.lang";
    private SharedPreferences prefs;
	private Button btnTutorial;
	private Button btnGames;
    private Intent tutorial;
    private Intent games;
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L10N.locale = new Locale(getPreferences().getString(LANG_KEY, "ru"));
        L10N.init(this.getResources());
        setContentView(R.layout.home);

        initBtnTutorial();
        initBtnGames();
    }
    
	@Override
	public void onClick(View arg0) {
    	if(arg0.equals(btnTutorial)) {
			startActivity(getTutorial());
    	} else if(arg0.equals(btnGames)) {
        	startActivity(getGames());
    	}
	}

	protected void setMenuBackground() {
	  getLayoutInflater().setFactory(new Factory() {
		  @Override
		  public View onCreateView(String name, Context context, AttributeSet attrs) {
		      if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
		        try {
		          LayoutInflater f = getLayoutInflater();
		          final View view = f.createView(name, null, attrs);
		          new Handler().post(new Runnable() {
		            public void run() {
		            	TextView text = (TextView) view; 
                        text.setBackgroundResource(R.drawable.button_shape);
                        text.setTextAppearance(Home.this, R.style.contextItem); 
                        text.setTypeface(L10N.font);
		            }
		          });
		          return view;
		        } catch (Exception e) {
		        }
		      }
		      return null;
		  }
	  });
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.language, menu);
        setMenuBackground();
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
		    case R.id.ru:
		    	changeLocale("ru");
		    	return true;
		    case R.id.ua:
		    	changeLocale("ua");
				return true;
		    default: finish();
		    	return true;
	    }
	}
	
	
    // PRIVATE METHODS
	
	private void changeLocale(String locale) {
		L10N.locale = new Locale(locale);
    	Editor edit = getPreferences().edit();
		edit.putString(LANG_KEY, locale);
		edit.commit();
		L10N.init(this.getResources());
		btnTutorial.setText(getResources().getString(R.string.tutorial));
		btnGames.setText(getResources().getString(R.string.quiz));
	}
    
	private SharedPreferences getPreferences() {
		if(prefs == null) {
			prefs = PreferenceManager.getDefaultSharedPreferences(this);
		}
		return prefs;
	}
	
    private void initBtnTutorial() {
		if(btnTutorial == null) {
			btnTutorial = (Button) findViewById(R.id.btnTutorial);
	        btnTutorial.setTypeface(L10N.font);
	        btnTutorial.setOnClickListener(this);
		}
	}
	
	private void initBtnGames() {
		if(btnGames == null) {
			btnGames = (Button) findViewById(R.id.btnGames);
	        btnGames.setTypeface(L10N.font);
	        btnGames.setOnClickListener(this);
		}
	}

    private Intent getTutorial() {
    	if(tutorial == null) {
    		tutorial = new Intent(this, Tutorial.class);
    	}
    	return tutorial;
    }
    
    private Intent getGames() {
    	if(games == null) {
    		games = new Intent(this, Games.class);
    	}
    	return games;
    }
    
}
